package com.mef.smsplat.web.controller.sys;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hc24.util.BeanUtilsEx;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.SmsHistoryInfoDomain;
import com.mef.smsplat.domain.SmsOpraInfoDomain;
import com.mef.smsplat.domain.SysAdminInfoDomain;
import com.mef.smsplat.service.SmsHistoryInfoService;
import com.mef.smsplat.service.SmsOpraInfoService;
import com.mef.smsplat.service.vo.PassengerVo;
import com.mef.smsplat.util.Constants;
import com.mef.smsplat.web.controller.base.Json;
import com.mef.smsplat.web.controller.qo.SmsHistoryQo;

@Controller
@RequestMapping("/sys/smshistory/")
public class SmsHistoryInfoController {
	private SmsHistoryInfoService smsHistoryInfoService;
	private SmsOpraInfoService smsOpraInfoService;

	public void setSmsHistoryInfoService(
			SmsHistoryInfoService smsHistoryInfoService) {
		this.smsHistoryInfoService = smsHistoryInfoService;
	}

	public void setSmsOpraInfoService(SmsOpraInfoService smsOpraInfoService) {
		this.smsOpraInfoService = smsOpraInfoService;
	}

	@ResponseBody
	@RequestMapping("query.sys")
	public Page<SmsHistoryInfoDomain> query(SmsHistoryQo qo, DataGridModel dgm) {
		return smsHistoryInfoService.query(qo, dgm);
	}

	@RequestMapping("queryDetail.sys")
	public ModelAndView queryDetail(String smsHistoryId) {
		SmsHistoryInfoDomain smsHistory= smsHistoryInfoService.getById(smsHistoryId);
		List<SmsOpraInfoDomain> smsOpraList=smsOpraInfoService.query(smsHistoryId);
		PassengerVo pass=new PassengerVo();
		BeanUtilsEx.copyProperties(pass, smsHistory);
		pass.setTelphone(smsHistory.getPhone());//手机号码
		ModelAndView av=new ModelAndView("page/sys/smshistory/history_detail");
		av.addObject("smsHistory", smsHistory);
		av.addObject("pass", pass);
		av.addObject("smsOpralist", smsOpraList);
		return av;
	}
	
	/**
	 * 终止本短信的发送
	 * @param session
	 * @param smsHistoryId
	 * @param remark
	 * @return
	 */
	@ResponseBody
	@RequestMapping("stopSms.sys")
	public Json stopSms(HttpSession session,String smsHistoryId,String remark){
		
		// 从Session中取得当前登录用户信息，当前是管理员
		SysAdminInfoDomain sys = (SysAdminInfoDomain) session
				.getAttribute(Constants.SESSION_SYS_ADMIN);
		SmsOpraInfoDomain smsOpra = new SmsOpraInfoDomain();
		if (sys != null) {
			smsOpra.setOpraId(sys.getSysAdminId());
			smsOpra.setOpraName(sys.getAdminName());
			smsOpra.setOpraPersonType(Constants.USER_TYPE_SYS);
			smsOpra.setRemark(remark);
		}
		smsHistoryInfoService.stopSms(smsHistoryId, smsOpra);
		
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("reSendSms.sys")
	public Json reSendSms(HttpSession session,String smsHistoryId,String telphone,String smsContent,String remark){
		// 从Session中取得当前登录用户信息，当前是管理员
		SysAdminInfoDomain sys = (SysAdminInfoDomain) session
				.getAttribute(Constants.SESSION_SYS_ADMIN);
		SmsOpraInfoDomain smsOpra = new SmsOpraInfoDomain();
		if (sys != null) {
			smsOpra.setOpraId(sys.getSysAdminId());
			smsOpra.setOpraName(sys.getAdminName());
			smsOpra.setOpraPersonType(Constants.USER_TYPE_SYS);
			smsOpra.setRemark(remark);
		}
		
		String smsId=smsHistoryInfoService.reSendSms(smsHistoryId, smsContent, telphone, smsOpra);
		
		Json json=new Json();
		json.setData(smsId);
		return json;
	}
}
